/* Gluten Library -- Camera
 *
 * A simple, first-person perspective camera.
 *
 * $AUTHOR$    res
 * $UPDATE$    r25
 */

/* LICENSES ********************************************************************

   Copyright (c) 2013- Reuben E. Smith

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.

 *******************************************************************************/


#include <cstring>
#include <GL/glew.h>
#include "Camera.hpp"
#include "Utility.hpp"


namespace Gluten
{
   const unsigned int M_SZ = 16;


   Camera::Camera()
      : _p(&_m[12]), _f(&_m[8]), _r(&_m[0]), _u(&_m[4])
   {
      std::memset(_m, 0, M_SZ * sizeof(float));

      // res: Reversed z-axis since positive z going forward from camera makes
      //      more sense to me.

      _m[15]   =  1.0;  // m(3, 3) must be 1
      _f[2]    = -1.0;  // forward = (0, 0, -1)
      _r[0]    =  1.0;  // right = (1, 0, 0)
      _u[1]    =  1.0;  // up = (0, 1, 0)
   }


   Camera::Camera(const Camera& other)
      : _p(&_m[12]), _f(&_m[8]), _r(&_m[0]), _u(&_m[4])
   {
      std::memcpy(_m, other._m, M_SZ * sizeof(float));
   }


   Camera::Camera(const Vector4f& pos, const Vector4f& rot)
      : _p(&_m[12]), _f(&_m[8]), _r(&_m[0]), _u(&_m[4])
   {
      std::memset(_m, 0, M_SZ * sizeof(float));

      _m[15]   =  1.0;
      _f[2]    = -1.0;
      _r[0]    =  1.0;
      _u[1]    =  1.0;

      _p[0] = pos(0);
      _p[1] = pos(1);
      _p[2] = pos(2);

      rotG(rot(1), 0, 1, 0);  // yaw
      rotG(rot(0), 1, 0, 0);  // pitch
      rotG(rot(2), 0, 0, 1);  // roll
   }


   Camera::Camera(float px, float py, float pz,
      float rx, float ry, float rz)
      : _p(&_m[12]), _f(&_m[8]), _r(&_m[0]), _u(&_m[4])
   {
      std::memset(_m, 0, M_SZ * sizeof(float));

      _m[15]   =  1.0;
      _f[2]    = -1.0;
      _r[0]    =  1.0;
      _u[1]    =  1.0;

      _p[0] = px;
      _p[1] = py;
      _p[2] = pz;

      rotG(ry, 0, 1, 0);
      rotG(rx, 1, 0, 0);
      rotG(rz, 0, 0, 1);
   }


   Camera& Camera::operator=(const Camera& other)
   {
      std::memcpy(_m, other._m, M_SZ * sizeof(float));
      return *this;
   }


   bool Camera::operator==(const Camera& other) const
   {
      for (unsigned int i = 0; i < M_SZ; ++i) {
         if (!fpRelativeEqual(_m[i], other._m[i])) {
            return false;
         }
      }

      return true;
   }


   bool Camera::operator!=(const Camera& other) const
   {
      return !operator==(other);
   }


   void Camera::view()
   {
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();

      GLfloat view[M_SZ] = {
         _r[0], _u[0], -_f[0], 0,
         _r[1], _u[1], -_f[1], 0,
         _r[2], _u[2], -_f[2], 0,

         -(_r[0] * _p[0] + _r[1] * _p[1] + _r[2] * _p[2]),
         -(_u[0] * _p[0] + _u[1] * _p[1] + _u[2] * _p[2]),
          (_f[0] * _p[0] + _f[1] * _p[1] + _f[2] * _p[2]),
         1
      };
      
      glLoadMatrixf(view);
   }


   void Camera::reset()
   {
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      
      GLfloat view[M_SZ] = {
         1,  0, -0, -0,
         0,  1, -0, -0,
         0,  0, -1,  0,
         0,  0,  0,  1
      };

      std::memcpy(_m, view, M_SZ);
      glLoadMatrixf(_m);
   }


   void Camera::resetPosition()
   {
      _p[0] = 0;
      _p[1] = 0;
      _p[2] = 0;
   }


   void Camera::resetRotation()
   {
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();

      GLfloat view[M_SZ] = {
         1,  0, -0, -_p[0],
         0,  1, -0, -_p[1],
         0,  0, -1,  _p[2],
         0,  0,  0,      1
      };

      std::memcpy(_m, view, M_SZ);
      glLoadMatrixf(_m);
   }


   Vector4f Camera::getForwardV() const
   {
      return Vector4f(_f);
   }


   void Camera::setForwardV(const Vector4f& v)
   {
      _f[0] = -v(0);
      _f[1] = -v(1);
      _f[2] = -v(2);
   }


   Vector4f Camera::getRightV() const
   {
      return Vector4f(_r);
   }


   void Camera::setRightV(const Vector4f& v)
   {
      _r[0] = v(0);
      _r[1] = v(1);
      _r[2] = v(2);
   }


   Vector4f Camera::getUpV() const
   {
      return Vector4f(_u);
   }


   void Camera::setUpV(const Vector4f& v)
   {
      _u[0] = v(0);
      _u[1] = v(1);
      _u[2] = v(2);
   }


   Vector4f Camera::getPosition() const
   {
      return Vector4f(_p);
   }


   void Camera::setPosition(const Vector4f& v)
   {
      _p[0] = v(0);
      _p[1] = v(1);
      _p[2] = v(2);
   }


   void Camera::setPosition(float x, float y, float z)
   {
      _p[0] = x;
      _p[1] = y;
      _p[2] = z;
   }


   void Camera::movG(float d, const Vector4f& axes)
   {
      _p[0] += d * axes(0);
      _p[1] += d * axes(1);
      _p[2] += d * axes(2);
   }


   void Camera::movG(float d, float aX, float aY, float aZ)
   {
      _p[0] += d * aX;
      _p[1] += d * aY;
      _p[2] += d * aZ;
   }


   void Camera::movL(float d, const Vector4f& axes)
   {
      _p[0] += d * (axes(0) * _r[0] + axes(1) * _u[0] + axes(2) * _f[0]);
      _p[1] += d * (axes(0) * _r[1] + axes(1) * _u[1] + axes(2) * _f[1]);
      _p[2] += d * (axes(0) * _r[2] + axes(1) * _u[2] + axes(2) * _f[2]);
   }


   void Camera::movL(float d, float aX, float aY, float aZ)
   {
      _p[0] += d * (aX * _r[0] + aY * _u[0] + aZ * _f[0]);
      _p[1] += d * (aX * _r[1] + aY * _u[1] + aZ * _f[1]);
      _p[2] += d * (aX * _r[2] + aY * _u[2] + aZ * _f[2]);
   }


   void Camera::rotG(float d, const Vector4f& axes)
   {
      glMatrixMode(GL_MODELVIEW);
      glPushMatrix();
      glLoadMatrixf(_m);
      glRotatef(d,
         axes(0) * _r[0] + axes(1) * _r[1] + axes(2) * _r[2],
         axes(0) * _u[0] + axes(1) * _u[1] + axes(2) * _u[2],
         axes(0) * _f[0] + axes(1) * _f[1] + axes(2) * _f[2]);
      glGetFloatv(GL_MODELVIEW_MATRIX, _m);
      glPopMatrix();
   }


   void Camera::rotG(float d, float aX, float aY, float aZ)
   {
      glMatrixMode(GL_MODELVIEW);
      glPushMatrix();
      glLoadMatrixf(_m);
      glRotatef(d,
         aX * _r[0] + aY * _r[1] + aZ * _r[2],
         aX * _u[0] + aY * _u[1] + aZ * _u[2],
         aX * _f[0] + aY * _f[1] + aZ * _f[2]);
      glGetFloatv(GL_MODELVIEW_MATRIX, _m);
      glPopMatrix();
   }


   void Camera::rotL(float d, const Vector4f& axes)
   {
      glMatrixMode(GL_MODELVIEW);
      glPushMatrix();
      glLoadMatrixf(_m);
      glRotatef(d, axes(0), axes(1), axes(2));
      glGetFloatv(GL_MODELVIEW_MATRIX, _m);
      glPopMatrix();
   }


   void Camera::rotL(float d, float aX, float aY, float aZ)
   {
      glMatrixMode(GL_MODELVIEW);
      glPushMatrix();
      glLoadMatrixf(_m);
      glRotatef(d, aX, aY, aZ);
      glGetFloatv(GL_MODELVIEW_MATRIX, _m);
      glPopMatrix();
   }
}

